<?php

if (!isset($website) ) { header('HTTP/1.1 404 Not Found'); die; }

if ( !is_logged() ) {

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : $phpbb_forum ;
$phpEx = substr(strrchr(__FILE__, '.'), 1);
if ( file_exists( $phpbb_forum .'common.php' ) ) {
include($phpbb_forum .'common.php');
     $user->session_begin();
     $auth->acl($user->data);
     $user->setup();
	 
     $phpbb_sid = $user->data['session_id'];
	 $phpbb_path = $phpbb_forum;
	 $phpbb_avatar = $phpbb_forum_url."images/avatars/gallery/".$user->data['user_avatar'];
	 $phpbb_userID = $user->data['user_id'];
	 $phpbb_userType = $user->data['user_type'];
	 $phpbb_userEmail = $user->data['user_email'];
	 $phpbb_logoutURL = $phpbb_forum_url."ucp.php?mode=logout&sid=".$phpbb_sid;
     $phpbb_user = trim($user->data['username']);

if ( $phpbb_userID>=1 AND $phpbb_userType!=2 ) {
	 require_once('inc/class.database.php');
	 require_once('inc/db_connect.php');
	 
	$check = $db->query("SELECT * FROM users WHERE user_email = '".$phpbb_userEmail."' AND phpbb_id = '".$phpbb_userID."'");

	if ( $db->num_rows($check)<=0 ) {
	//CREATE NEW USER (from phpbb database)
	
	 $checkUn = $db->query("SELECT * FROM users WHERE LOWER(user_name) = LOWER('".$phpbb_user."') ");
	 if ( $db->num_rows($checkUn)>=1 ) {
	    $phpbb_user = $phpbb_user."_".$phpbb_userID;
	 }
	
	 $pass = generate_hash(5);
     $hash = generate_hash(12);
	 $password_db = generate_password($pass, $hash); 
	   $insert = $db->query("INSERT INTO users(user_name, user_email, user_password, password_hash, user_joined, user_level, user_last_login, user_ip, user_avatar, phpbb_id )
	   VALUES('".$phpbb_user."', '".$phpbb_userEmail."', '".$password_db."', '".$hash."', '".(int) time()."', '0', '".(int) time()."', '".safeEscape($_SERVER["REMOTE_ADDR"])."', '".$phpbb_avatar."', '".$phpbb_userID."' )");
	   $id = $db->get_insert_id();
	   $_SESSION["user_id"] = $id ;
	   $_SESSION["username"] =$phpbb_user;
	   $_SESSION["email"]    = $phpbb_userEmail;
	   $_SESSION["level"]    = 0;
	   $_SESSION["can_comment"]    = 1;
	   $_SESSION["logged"]    = time();
	   $_SESSION["phpbb"]    = $phpbb_userID;
	   $_SESSION["sid"]    = $phpbb_sid;
	   $_SESSION["logout"]    = $phpbb_logoutURL;
	} else {
	  //UPDATE USER DATA
	  $row = $db->fetch_array($check,'assoc');
	  
	  $update = $db->query("UPDATE users SET 
	  user_last_login = '".(int) time()."',
	  user_avatar = '".$phpbb_avatar."'
	  WHERE user_id = '".$row["user_id"]."' LIMIT 1");
	   $_SESSION["user_id"] = $row["user_id"] ;
	   $_SESSION["username"] = $row["user_name"];
	   $_SESSION["email"]    = $phpbb_userEmail;
	   $_SESSION["level"]    = $row["user_level"];
	   $_SESSION["can_comment"]    = 1;
	   $_SESSION["logged"]    = time();
	   $_SESSION["phpbb"]    = $phpbb_userID;
	   $_SESSION["sid"]    = $phpbb_sid;
	   $_SESSION["logout"]    = $phpbb_logoutURL;
	  }
	
   }
	 
 }

}
?>